package service;

import java.util.ArrayList;

import dao.CarrierDAO;
import dao.CustomerReviewDAO;
import dao.DeviceBrandDAO;
import dao.OperatingSystemDAO;
import dao.PriceDAO;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import pojo.Carrier;
import pojo.CustomerReview;
import pojo.DeviceBrand;
import pojo.OperatingSystem;
import pojo.Price;

public class Tools {
	
	private static Tools _instance = null;
	private Tools()
	{}
	
	public static Tools getInstance()
	{
		if (_instance == null)
			_instance = new Tools();
		return _instance;
	}
	
	public List<String> parseString(String str, Character c)
	{
		List<String> lststr = new ArrayList<String>();
		int s = str.indexOf(c);
		while ( s > 0)
		{
			lststr.add(str.substring(0, s));
			str = str.substring(s+1);
			s = str.indexOf(c);
		}
		return lststr;
	}
	
	public List findListPriceFromNarrow(String narrow, String str, PriceDAO PriceDAO)
	{
		if (str.indexOf("price") != -1)
		{
			List<Price> lstPrice = new ArrayList<Price>();
			int s = narrow.indexOf(str);
			while (s != -1)
			{
				narrow = narrow.substring(s + str.length());
				
				int idprice;
				
				int s1 = narrow.indexOf("~");
				if (s1 == -1)
				{
					idprice = Integer.parseInt(narrow);
					narrow = "";
				}
				else
				{
					String str1 = narrow.substring(0, s1);
					idprice = Integer.parseInt(str1);
					narrow = narrow.substring(s1 + 1);
				}
				Price price = PriceDAO.getPriceByID(idprice);
				if ( price != null)
					lstPrice.add(price);
				s = narrow.indexOf(str);
			}
			return lstPrice;
		}
			
		
		return null;
	}
	
	public List findListDeviceBrandFromNarrow(String narrow, String str, DeviceBrandDAO DeviceBrandDAO)
	{
		if (str.indexOf("devicebrand") != -1)
		{
			List<DeviceBrand> lstDeviceBrand = new ArrayList<DeviceBrand>();
			int s = narrow.indexOf(str);
			while (s != -1)
			{
				narrow = narrow.substring(s + str.length());
				
				int idprice;
				
				int s1 = narrow.indexOf("~");
				if (s1 == -1)
				{
					idprice = Integer.parseInt(narrow);
					narrow = "";
				}
				else
				{
					String str1 = narrow.substring(0, s1);
					idprice = Integer.parseInt(str1);
					narrow = narrow.substring(s1 + 1);
				}
				DeviceBrand db = DeviceBrandDAO.getDeviceBrandByID(idprice);
				if ( db != null)
					lstDeviceBrand.add(db);
				s = narrow.indexOf(str);
			}
			return lstDeviceBrand;
		}
			
		
		return null;
	}
	
	public List findListCarrierFromNarrow(String narrow, String str, CarrierDAO CarrierDAO)
	{
		if (str.indexOf("carrier") != -1)
		{
			List<Carrier> lstCarrier = new ArrayList<Carrier>();
			int s = narrow.indexOf(str);
			while (s != -1)
			{
				narrow = narrow.substring(s + str.length());
				
				int id;
				
				int s1 = narrow.indexOf("~");
				if (s1 == -1)
				{
					id = Integer.parseInt(narrow);
					narrow = "";
				}
				else
				{
					String str1 = narrow.substring(0, s1);
					id = Integer.parseInt(str1);
					narrow = narrow.substring(s1 + 1);
				}
				Carrier ca = CarrierDAO.getCarrierByID(id);
				if ( ca != null)
					lstCarrier.add(ca);
				s = narrow.indexOf(str);
			}
			return lstCarrier;
		}
			
		
		return null;
	}
	
	public List findListOSFromNarrow(String narrow, String str, OperatingSystemDAO OperatingSystemDAO)
	{
		if (str.indexOf("os") != -1)
		{
			List<OperatingSystem> lstOperatingSystem = new ArrayList<OperatingSystem>();
			int s = narrow.indexOf(str);
			while (s != -1)
			{
				narrow = narrow.substring(s + str.length());
				
				int id;
				
				int s1 = narrow.indexOf("~");
				if (s1 == -1)
				{
					id = Integer.parseInt(narrow);
					narrow = "";
				}
				else
				{
					String str1 = narrow.substring(0, s1);
					id = Integer.parseInt(str1);
					narrow = narrow.substring(s1 + 1);
				}
				OperatingSystem os = OperatingSystemDAO.getOSByID(id);
				if ( os != null)
					lstOperatingSystem.add(os);
				s = narrow.indexOf(str);
			}
			return lstOperatingSystem;
		}
			
		
		return null;
	}
	
	public List findListCustomerReviewFromNarrow(String narrow, String str, CustomerReviewDAO CustomerReviewDAO)
	{
		if (str.indexOf("rate") != -1)
		{
			List<CustomerReview> lstCustomerReview = new ArrayList<CustomerReview>();
			int s = narrow.indexOf(str);
			while (s != -1)
			{
				narrow = narrow.substring(s + str.length());
				
				int id;
				
				int s1 = narrow.indexOf("~");
				if (s1 == -1)
				{
					id = Integer.parseInt(narrow);
					narrow = "";
				}
				else
				{
					String str1 = narrow.substring(0, s1);
					id = Integer.parseInt(str1);
					narrow = narrow.substring(s1 + 1);
				}
				CustomerReview cr = CustomerReviewDAO.getCustomerReviewByID(id);
				if ( cr != null)
					lstCustomerReview.add(cr);
				s = narrow.indexOf(str);
			}
			return lstCustomerReview;
		}
			
		
		return null;
	}
}
